package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lsy.weizhi2.R;
import com.lsy.weizhi2.config.Classify;
import com.lsy.weizhi2.customview.MyListView;
import com.lsy.weizhi2.myutils.ClassifyFirstCall;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 嗨哟 on 2017/3/17.
 */
public class Classify_First_Adapter extends RecyclerView.Adapter<Classify_First_Adapter.Classify_First_Holder> {
    private Context conetxt;
    private List<com.lsy.weizhi2.JsonObject.Classify> list;
    private List<com.lsy.weizhi2.JsonObject.Classify.SecondCategoriesBean>list_s;
    private int position;
    private LayoutInflater layoutInflater;
    private ClassifyFirstCall classifyFirstCall;
    public Classify_First_Adapter(Context conetxt, List<com.lsy.weizhi2.JsonObject.Classify> list , int position) {
        this.conetxt = conetxt;
        this.list = list;
        this.position = position;
        layoutInflater=LayoutInflater.from(conetxt);
    }

    public void UpData(List<com.lsy.weizhi2.JsonObject.Classify> list,int position){
        this.list=list;
        this.position=position;
        this.notifyDataSetChanged();
    }

    public void setClassifyFirstCall(ClassifyFirstCall classifyFirstCall){
        this.classifyFirstCall=classifyFirstCall;
    }

    @Override
    public Classify_First_Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=layoutInflater.inflate(R.layout.classify_first_item,parent,false);
        AutoUtils.autoSize(view);
        return new Classify_First_Holder(view);
    }

    @Override
    public void onBindViewHolder(Classify_First_Holder holder, int position) {
        holder.position=position;
        if(this.position==position){
            holder.first_item_iv.setImageResource(Classify.first_1[position]);
            holder.first_item_iv.setBackgroundResource(R.color.white);
            if(list!=null){
                list_s=new ArrayList<>();
                list_s=list.get(position).getSecondCategories();
            }
            Log.i("ClassifyAdapter","position=="+position);
            Log.i("ClassifyAdapter","list_s=="+list_s.size());
            Classify_List_Adapter classify_list_adapter=new Classify_List_Adapter(list_s,position);
            holder.first_item_mlv.setAdapter(classify_list_adapter);
        }else{
            holder.first_item_iv.setImageResource(Classify.first_2[position]);
            holder.first_item_iv.setBackgroundResource(R.color.color_f5);
            Classify_List_Adapter classify_list_adapter=new Classify_List_Adapter(null,position);
            holder.first_item_mlv.setAdapter(classify_list_adapter);
        }
    }

    @Override
    public int getItemCount() {
        return list==null?8:list.size();
    }

    class Classify_First_Holder extends RecyclerView.ViewHolder{
        int position;
        ImageView first_item_iv;
        MyListView first_item_mlv;
        public Classify_First_Holder(View itemView) {
            super(itemView);
            first_item_iv= (ImageView) itemView.findViewById(R.id.first_item_iv);
            first_item_mlv= (MyListView) itemView.findViewById(R.id.first_item_mlv);
            first_item_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i("ClassifyAdapter","position=first="+position);
                    UpData(list,position);
                    //更新同时获取数据改变显示
                    classifyFirstCall.GetData(list.get(position).getName(),position+1);
                }
            });
        }
    }

    class Classify_List_Adapter extends BaseAdapter{
        List<com.lsy.weizhi2.JsonObject.Classify.SecondCategoriesBean>list;
        int position;
        public Classify_List_Adapter(List<com.lsy.weizhi2.JsonObject.Classify.SecondCategoriesBean>list,int position) {
            this.list=list;
            this.position=position;
        }

        public void UpData(int position){
            this.position=position;
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return list==null?0:list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            final ListItemHolder holder;
            if(view==null){
                view=layoutInflater.inflate(R.layout.text_view2,viewGroup,false);
                holder=new ListItemHolder();
                holder.textView= (TextView) view.findViewById(R.id.textView);
                holder.textline=view.findViewById(R.id.textline);
                AutoUtils.autoSize(view);
                view.setTag(holder);
            }else{
                holder= (ListItemHolder) view.getTag();
            }

            holder.textView.setText(list.get(i).getName());
            if(position==i){
                holder.textView.setTextColor(conetxt.getResources().getColor(R.color.clicked_ok));
                holder.textline.setVisibility(View.VISIBLE);
            }else{
                holder.textView.setTextColor(conetxt.getResources().getColor(R.color.classify_text));
                holder.textline.setVisibility(View.INVISIBLE);
            }
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    UpData(i);
                    classifyFirstCall.onClick(view,holder.textView.getText().toString(),list.get(i).getId());
                }
            });
            return view;
        }

        class ListItemHolder{
            TextView textView;
            View textline;
        }
    }
}
